<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>商品详情页</title>
</head>
<body>
	<%@include file="/WEB-INF/jsp/top.jsp" %>
	<h6><a href="${pageContext.request.contextPath }/index2">回到主页</a></h6>
	<h6><c:if test="${store.cid==1}"><a href="${pageContext.request.contextPath }/store/matong">回到马桶页</a></c:if>
		<c:if test="${store.cid==2}"><a href="${pageContext.request.contextPath }/store/shafa">回到沙发页</a></c:if></h6>
	<div>
		<c:if test="${store.cid==1}"><img alt="马桶" src="${pageContext.request.contextPath }/statics/img/matong.JPG" height="200" width="150"></c:if>
		<c:if test="${store.cid==2}"><img alt="沙发" src="${pageContext.request.contextPath }/statics/img/shafa.JPG" height="200" width="150"></c:if>
	</div>
	<div>商品名字：${store.storeName}</div>
	<div>这是商品描述，以后再放数据库里做补充</div>
	<div>商品价格：${store.price}</div>
	<div>商品销量：${store.sales}</div>
	<div>数量：<input onkeyup="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}" onafterpaste="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}" id="i" type="number" name="ii" value="1" onblur="shiqu(${store.price })">,总计：<span id="s">${store.price }</span></div>
	<div><button onclick="addOrders()">立即购买</button><button onclick="addCarItem()">加入购物车</button></div><hr>
	<shiro:hasRole name="pingjia:delete">
		<div><button type="button" onclick="deleteBatch()">批量删除</button></div>
	</shiro:hasRole>
	<div><table border="1" id="tab1">
		<th>评价用户</th><th>评价时间</th><th>评价星级</th><th>评价内容</th><th>收货时间</th></tr>
	</table></div>
	<div id="div2"></div>
	<div id="div1"></div>
</body>
<script type="text/javascript" src="${pageContext.request.contextPath }/statics/js/jquery-3.5.1.js"></script>
<script type="text/javascript">
	function addCarItem(){
		var jsonObj={"sid":${store.sid},"storeName":"${store.storeName}","price":${store.price},"number":${store.number},
				"sales":${store.sales},"cid":${store.cid},"count":$("#i").val(),"subtotal":$("#i").val()*${store.price}};
		$.ajax({
			type:"post",
			url:"${pageContext.request.contextPath}/car/insertCarItem",
			data:jsonObj,
			success:function(result){
				if(result=="ok"){
					alert("添加购物车成功");
					window.location.href="${pageContext.request.contextPath}/store/store?id=${store.sid}";
				}
			}
		});
	}
	
	/* function addOrders(){
		var jsonObj={"sid":${store.sid},"count":$("#i").val(),"subtotal":${store.price}*($("#i").val())};
		alert($("#i").val());
		alert(${store.price}*($("#i").val()));
		$.ajax({
			type:"post",
			url:"${pageContext.request.contextPath}/orders/insertOrdersOne",
			data:jsonObj,
			async:false,
			success:function(result){
				if(result=="orders/saoma"){
					alert("加入我的订单成功，应该是去付款页面");
				} 
			}
		});
	} */
	function addOrders(){
		window.location.href="${pageContext.request.contextPath}/orders/insertOrdersOne?sid=${store.sid}&count="
			+$("#i").val()+"&subtotal="+${store.price}*($("#i").val());
	}

	function shiqu(suibian){
		var i=document.getElementById("i").value;//找到原因了，是因为id属性值重复了，所以模棱两可的值为on
		if(i<1){
			$("#i").val(1);
			//document.getElementById("i").value=1;
			$("#s").html(suibian);
		}else if(i>99){
			document.getElementById("i").value=99;
			$("#s")[0].innerHTML=suibian*99;
			//$("#s").html(suibian*99);
		}else{
			document.getElementById("s").innerHTML=suibian*document.getElementById("i").value;
			//$("#s").html(suibian*document.getElementById("i").value);
		}
	}

	$(function(){
		start(1);
	});
	var jsonObj={"pageNum":1,"sid":${store.sid}};
	function start(start){
		jsonObj.pageNum=start;
		$.ajax({
			type:"post",
			data:jsonObj,
			url:"${pageContext.request.contextPath}/pingjia/selectAllPingjiaBySid",
			success:function(result){
				var object="<tr><shiro:hasRole name='pingjia:delete'><th><input type='checkbox' id='allCheckbox' onclick='allCheck()'>"
					+"</th></shiro:hasRole>"
					+"<th>评价用户</th><th>评价时间</th><th>评价星级</th><th>评价内容</th><th>收货时间</th>";
					object+="<shiro:hasRole name='pingjia:delete'><th>操作</th><th>高级操作</th></shiro:hasRole></tr>";
				$.each(result.list,function(i,n){
					object+="<tr><shiro:hasRole name='pingjia:delete'><td><input type='checkbox' id='"+n.pid+"'></td></shiro:hasRole>"
						+"<td>"+n.username+"</td><td>"+n.pingjiaTime+"</td><td>"+n.star;
						if(n.star==1){
							object+="(差评)";
						}else if(n.star>1 && n.star<5){
							object+="(中评)";
						}else{
							object+="(好评)";
						}
					object+="</td><td>"+n.content+"</td><td>"+n.orderTime;
					object+="</td><shiro:hasRole name='pingjia:delete'><td><a href='#' onclick='deletePingjiaByPid("
						+n.pid+")'>删除此评价</a></td><td><a href='#' onclick='deletePingjiaByUser("
						+n.uid+","+n.sid+")'>删除此用户在本商品内的所有评价</a>||<a href='#' onclick='deleteAllPingjiaByUser("
						+n.uid+")'>删除此用户的所有商品的评价</a></td></shiro:hasRole></tr>";
							
						/* if(n.uid==${uid}){
							object+="</td><td><a href='#' onclick=deletePingjia('"+n.sid
								+"')>删除</a></td><tr>";
							object+="</td><td></td></tr>";
						}else{
							object+="</td><td></td></tr>";
						} */
						
				});	
				$("#tab1").html(object);
				
				var object1="当前第"+result.pageNum+"页，总共"+result.pages+"页，总共"+result.total+"条记录";
				$("#div2").html(object1);
				//为了删除数据回到原来页面，而不是回到首页
				$("#div2").prop("is",result.pageNum);
				$("#div2").prop("nonce",result.size);
				
				
				var object2="";
				if(result.isFirstPage){
					object2+="<a>首页</a>";
				}else{
					object2+="<a href='#' onclick='pageChange(1)'>首页</a>";
				}
				if(result.hasPreviousPage){
					object2+="<a href='#' onclick='pageChange("+(result.pageNum-1)+")'>&laquo;</a>";
				}
				$.each(result.navigatepageNums,function(i,n){
					if(n==result.pageNum){
						object2+="<a>"+n+"</a>";
					}else{
						object2+="<a href='#' onclick='pageChange("+n+")'>"+n+"</a>";
					}
				});	
				if(result.hasNextPage){
					object2+="<a href='#' onclick='pageChange("+(result.pageNum+1)+")'>&raquo;</a>";
				}
				if(result.isLastPage){
					object2+="<a>末页</a>";
				}else{
					object2+="<a href='#' onclick='pageChange("+result.pages+")'>末页</a>";
				}
				$("#div1").html(object2);
			}
		});
	}
	
	function pageChange(currentPage){
		start(currentPage);
	}

	function deletePingjiaByPid(suibian){
		alert(suibian);
		$.ajax({
			type:"post",
			url:"${pageContext.request.contextPath}/pingjia/deletePingjiaByList",
			data:{["pingjiaList[0].pid"]:suibian},
			success:function(result){
				if(result>0){
					alert("成功删除");
					if($("#div2")[0].nonce==result){//当前页数量等于删除的记录数
						start($("#div2")[0].is-1);
					}else{
						start($("#div2")[0].is);
					}
				}
				if(result==-1){
					alert("用户已取消删除");
				}
			}
		});
	}

	function deleteBatch(){
		var all=$("table tr td input:checked");
		if(all.length==0){
			alert("没有要删除的对象，请重新选择");
			return false;
		}
		var jsonObj2={};
		$.each(all,function(i,n){
			jsonObj2["pingjiaList["+i+"].pid"]=n.id;
		});
		$.ajax({
			type:"post",
			url:"${pageContext.request.contextPath}/pingjia/deletePingjiaByList",
			data:jsonObj2,
			success:function(result){
				if(result>0){
					alert("删除"+result+"条记录成功");
					if($("#div2")[0].nonce==result){//当前页数量等于删除的记录数
						start($("#div2")[0].is-1);
					}else{
						start($("#div2")[0].is);
					}
				}
				if(result==-1){
					alert("用户已取消删除");
				}
			}
		});
	}
	function deletePingjiaByUser(sui,bian){
		alert(sui+","+bian);
		$.ajax({
			type:"post",
			url:"${pageContext.request.contextPath}/pingjia/deletePingjiaByUser",
			data:{
				"uid":sui,"sid":bian
			},
			success:function(result){
				if(result>0){
					alert("成功删除"+result);
					if($("#div2")[0].nonce==result){//当前页数量等于删除的记录数
						start($("#div2")[0].is-1);
					}else{
						start($("#div2")[0].is);
					}
				}
				if(result==-1){
					alert("用户已取消删除");
				}
			}
		});
	}
	function deleteAllPingjiaByUser(suibian){
		alert(suibian);
		$.ajax({
			type:"post",
			url:"${pageContext.request.contextPath}/pingjia/deletePingjiaByUser",
			data:{"uid":suibian},
			success:function(result){
				if(result>0){
					alert("成功删除"+result);
					if($("#div2")[0].nonce==result){//当前页数量等于删除的记录数
						start($("#div2")[0].is-1);
					}else{
						start($("#div2")[0].is);
					}
				}
				if(result==-1){
					alert("用户已取消删除");
				}
			}
		});
	}
</script>
</html>